var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var _ = require("lodash");


var indexRouter = require('./routes/index');
var loginRouter = require('./routes/login');
var isEquipRecordedRouter = require('./routes/isEquipRecorded');
var addNewEquipmentRouter = require('./routes/addNewEquipment');
var getEquipmentInfoRouter = require('./routes/getEquipmentInfo');
var getEquipmentBaseInfoByBHRouter = require('./routes/getEquipmentBaseInfoByBH');
var getEquipmentBaseInfoByIdRouter = require('./routes/getEquipmentBaseInfoById');
var getEquipmentStateInfoRouter = require('./routes/getEquipmentStateInfo');
var getToDoListRouter = require('./routes/getToDoList');
var addHitchRouter = require('./routes/addHitch');
var equipmentEventPassOnRouter = require('./routes/equipmentEventPassOn');
var closeHitchRouter = require('./routes/closeHitch');
var addMaintainRouter = require('./routes/addMaintain');
var closeMaintainRouter =  require('./routes/closeMaintain');
var addChangeRouter =  require('./routes/addChange');
var scrapRouter =  require('./routes/scrapEquip');
var addEventRouter = require('./routes/addEvent');
var addDemandRouter = require('./routes/addDemand');
var getEquipEventRouter = require('./routes/getEquipEvent');
var getEventByRoleRouter = require('./routes/getEventsByRole');
var equipmentSumCountRouter = require('./routes/equipmentSumCount');
var closeEventRouter = require('./routes/closeEvent');
var closeDemandRouter = require('./routes/closeDemand');
var queryDataRouter = require('./routes/queryData');

var systemStandDataRouter = require('./routes/getSystemStandData');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 请求头配置
app.use(function(req, res, next) {
  // res.setHeader("Access-Control-Allow-Origin", "*");
  // res.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,PUT,PATCH,DELETE');
  // res.setHeader("Access_Control-Allow-Headers", "origin, X-Requested-With,Content-Type,Accept,Access-Token");
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Credentials", "true");
  res.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,PATCH,DELETE");
  res.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token,Authorization");
  res.setHeader("Access-Control-Expose-Headers", "*");
  next();
})

app.use('/', indexRouter);
app.use('/login', loginRouter);
app.use('/isEquipRecorded', isEquipRecordedRouter);
app.use('/addNewEquipment', addNewEquipmentRouter);
app.use('/getEquipmentInfo', getEquipmentInfoRouter);
app.use('/getEquipmentBaseInfoByBH', getEquipmentBaseInfoByBHRouter);
app.use('/getEquipmentBaseInfoById', getEquipmentBaseInfoByIdRouter);
app.use('/getEquipmentStateInfo', getEquipmentStateInfoRouter);
app.use('/getToDoList', getToDoListRouter);
app.use('/addHitch', addHitchRouter);
app.use('/equipmentEventPassOn', equipmentEventPassOnRouter);
app.use('/closeHitch', closeHitchRouter);
app.use('/addMaintain', addMaintainRouter);
app.use('/closeMaintain', closeMaintainRouter);
app.use('/addChange', addChangeRouter);
app.use('/scrapEquip', scrapRouter);
// app.use('/getDepart', departRouter);
// app.use('/getBrand', brandRouter);
// app.use('/getEquipmentType', equipmentTypeRouter);
// app.use('/getWorkshop', workshopRouter);
// app.use('/getDemandType', demandTypeRouter);
// app.use('/getDemandProcess',demandProcessRouter);
// app.use('/getCorSystems',corSystemsRouter);
app.use('/getEquipEvent', getEquipEventRouter);
app.use('/getEventByRole', getEventByRoleRouter);
app.use('/equipmentSumCount', equipmentSumCountRouter);
app.use('/addEvent', addEventRouter);
app.use('/addDemand', addDemandRouter);
app.use('/closeEvent', closeEventRouter);
app.use('/closeDemand', closeDemandRouter);
app.use('/queryData', queryDataRouter);

app.use('/getSystemStandData', systemStandDataRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});


// nodemon 热更新
var debug = require('debug')('my-application'); // debug模块
app.set('port', process.env.PORT || 3000); // 设定监听端口

var server = app.listen(app.get('port'), function() {
  //启动监听
  debug('Express server listening on port ' + server.address().port);
});
// module.exports = app;
